package com.pgeg.mixin.entity;

import net.minecraft.block.GrassBlock;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(AnimalEntity.class)
public abstract class AnimalEntityMixin extends PassiveEntity {

    public AnimalEntityMixin(EntityType<? extends PassiveEntity> entityType, World world) {
        super(entityType, world);
    }

    public boolean canSpawn(WorldAccess world) {
        int x = MathHelper.floor(this.getBlockPos().getX());
        int y = MathHelper.floor(this.getBoundingBox().minY);
        int z = MathHelper.floor(this.getBlockPos().getZ());
        BlockPos pos = new BlockPos(x, y, z);
        return world.getBlockState(
                pos.down()).getBlock() instanceof GrassBlock
                && world.getBaseLightLevel(pos, 0) > 8 && super.canSpawn(world);
    }

}
